﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GerbilLib;
using System.Threading.Tasks;

namespace MediaGerbil
{
    public partial class TaskManagerDialog : Form
    {
        private BindingList<DisplayTask> DisplayTasks = new BindingList<DisplayTask>();

        public TaskManagerDialog()
        {
            InitializeComponent();
            InitializeTasksDataGridView();
            TaskManager.TaskAddedEvent += new EventHandler<TaskEventArgs>(TaskManager_TaskAddedEvent);
            TaskManager.TaskRemovedEvent += new EventHandler<TaskEventArgs>(TaskManager_TaskRemovedEvent);
        }

        void TaskManager_TaskAddedEvent(object sender, TaskEventArgs e)
        {
            if (InvokeRequired)
                BeginInvoke(new MethodInvoker(() => DisplayTasks.Add(new DisplayTask(e.Task, e.TaskName, DateTime.Now))));
            else
                DisplayTasks.Add(new DisplayTask(e.Task, e.TaskName, DateTime.Now));
        }

        void TaskManager_TaskRemovedEvent(object sender, TaskEventArgs e)
        {
            DisplayTask task = FindDisplayTask(e.Task);
            if (task != null)
            {
                task.Ended = DateTime.Now;
                UpdateTasksDataGridView();
            }
        }

        private void InitializeTasksDataGridView()
        {
            tasksDataGridView.AutoGenerateColumns = false;

            // Task Id
            DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
            id.Name = "TaskId";
            id.DataPropertyName = "Id";
            id.HeaderText = "Task ID";
            tasksDataGridView.Columns.Add(id);

            // Task Name
            DataGridViewTextBoxColumn taskName = new DataGridViewTextBoxColumn();
            taskName.Name = "TaskName";
            taskName.DataPropertyName = "Name";
            taskName.HeaderText = "Task Name";
            tasksDataGridView.Columns.Add(taskName);

            // Status
            DataGridViewTextBoxColumn status = new DataGridViewTextBoxColumn();
            status.Name = "Status";
            status.DataPropertyName = "Status";
            status.HeaderText = "Status";
            tasksDataGridView.Columns.Add(status);

            // Started At
            DataGridViewTextBoxColumn started = new DataGridViewTextBoxColumn();
            started.Name = "StartedAt";
            started.DataPropertyName = "Started";
            started.HeaderText = "Started At";
            tasksDataGridView.Columns.Add(started);

            // Ended At
            DataGridViewTextBoxColumn ended = new DataGridViewTextBoxColumn();
            ended.Name = "EndedAt";
            ended.DataPropertyName = "Ended";
            ended.HeaderText = "Ended At";
            tasksDataGridView.Columns.Add(ended);

            tasksDataGridView.DataSource = DisplayTasks;
        }

        private DisplayTask FindDisplayTask(Task task)
        {
            foreach (DisplayTask displayTask in DisplayTasks)
                if (displayTask.Task == task) return displayTask;

            return null;
        }

        private void UpdateTasksDataGridView()
        {
            if (InvokeRequired)
                BeginInvoke(new MethodInvoker(() => tasksDataGridView.Refresh()));
            else
                tasksDataGridView.Refresh();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //DisplayTask currentTask = tasksDataGridView.CurrentRow.DataBoundItem as DisplayTask;
            //TaskManager.CancelTask(currentTask.Task);
            TaskManager.Run("Test Thread", () =>
            {
                System.Threading.Thread.Sleep(5000);
            });
        }

        private void btnClearFinished_Click(object sender, EventArgs e)
        {
            List<DisplayTask> finishedTasks = new List<DisplayTask>();
            foreach (DisplayTask task in DisplayTasks)
                if (task.Task.Status == TaskStatus.RanToCompletion) finishedTasks.Add(task);
            
            foreach (DisplayTask task in finishedTasks)
                DisplayTasks.Remove(task);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            UpdateTasksDataGridView();
        }
    }

    internal class DisplayTask
    {
        public Task Task { get; set; }
        public int Id
        {
            get { return Task.Id; }
        }
        public string Name { get; set; }
        public string Status
        {
            get { return Task.Status.ToString(); }
        }
        public DateTime Started { get; set; }
        public DateTime Ended { get; set; }

        public DisplayTask(Task task, string taskName, DateTime startedAt)
        {
            Task = task;
            Name = taskName;
            Started = startedAt;
        }
    }
}
